import Vue from 'vue'
import Router from 'vue-router'
import approvalsRouter from './modules/approvals'
import departmentsRouter from './modules/departments'
import employeesRouter from './modules/employees'
import permissionRouter from './modules/permission'
import attendancesRouter from './modules/attendances'
import salarysRouter from './modules/salarys'
import settingRouter from './modules/setting'
import socialRouter from './modules/social'

Vue.use(Router)

/* Layout */
// 父路由的组件
import Layout from '@/layout'

/**
 * constantRoutes 静态路由（写死的，固定的）| 动态路由（根据用户权限动态添加）
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
// 配置路由规则
// 1.动态路由 需要做权限控制的路由，用户如果权限不一致访问到的路由也不一样
export const asyncRoutes = [
  departmentsRouter,
  settingRouter,
  employeesRouter,
  permissionRouter,
  approvalsRouter,
  attendancesRouter,
  salarysRouter,
  socialRouter

]

// 2.静态路由  不需要做权限控制的路由，每个用户都可以正常访问
export const constantRoutes = [
  {
    path: '/login',
    // 路由懒加载
    component: () => import('../views/login/index.vue'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },
  // 员工批量导入
  {
    path: '/import',
    component: Layout,
    children: [
      {
        path: '',
        component: () => import('@/views/import')
      }
    ]
  },
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: 'dashboard',
      component: () => import('@/views/dashboard/index'),
      meta: { title: '首页', icon: 'dashboard' }
    }]
  }

  // 404 page must be placed at the end !!!
  // { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  // 每次切换页面，对页面的滚动条滚动位置重置为0
  scrollBehavior: () => ({ y: 0 }),
  // 路由的配置数组
  // 合并静态和动态路由的配置
  routes: [...constantRoutes]
})
// 创建路由实例
const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
// 重置路由
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
